package br.com.unitunes.dao;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import br.com.caelum.vraptor.ioc.Component;
import br.com.unitunes.model.Configuracao;
import br.com.unitunes.model.SistemaConf;
import br.com.unitunes.util.HibernateUtil;

@Component
public class SistemaDAO {
	
	private final EntityManager entityManager = HibernateUtil.getEntityManagerFactory().createEntityManager();
	
	public Class<Configuracao> getModelClass() {
		return Configuracao.class;
	}
	
	public String getValorConfig(SistemaConf emailmainadmin) {		
		Query query = entityManager.createQuery("SELECT config FROM CONFIG config WHERE UPPER(config.configuracao) LIKE UPPER(:configuracao)");
		query.setParameter("configuracao", emailmainadmin);
		
		try {
			Configuracao configuracao = (Configuracao) query.getSingleResult(); 
			return configuracao.getValor();
								
		} catch (Exception NoResultException) {
			return null;
		}		
	}
	
    public void saveConfig(Configuracao config) {
    	entityManager.getTransaction().begin();
    	config = entityManager.merge(config);
    	entityManager.getTransaction().commit();
    }	
}
